package com.firefightingiot_data.data.web.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.firefightingiot_data.data.common.base.BaseService;
import com.firefightingiot_data.data.common.utils.appkit.ConfigKit;
import com.firefightingiot_data.data.common.utils.appkit.FirefightingiotKit;
import com.firefightingiot_data.data.support.model.RespEntity;
import com.firefightingiot_data.data.support.utils.MyLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: YangYu
 * @ClassName: FireAddAlarmService
 * @Date: Created in 18:44 2021/5/12
 * @Description:
 */

@Service
@Slf4j
public class FireAddAlarmService extends BaseService {

    @Resource
    public MyLogger logger;

    @Async
    public void alarmAddFile(JSONObject jsonParam) throws InterruptedException {
        Thread.sleep(1000);
        logger.info("------------------------------------alarmAddFile:"+jsonParam.toString() +"-----------------------------------------------------");
        logger.info("------------------------------------alarmAddFile:"+"-----------------------------------------------------");
        //调用自己的这个方法，调用BaseService中的rest方法会因httpServletRequest报错
        String fireFlag = ConfigKit.getStr("fireFlag","config");
        if(fireFlag.equals("1")){
            fireFightingIotJsonMy(jsonParam, "fireBrigade", "alarmData", "jointFire",jsonParam.getStr("xftoken"));
        }

    }
    public RespEntity fireFightingIotJsonMy(JSONObject jsonParam, String configKey, String key, String gp, String token){
        String ip = ConfigKit.getStr(configKey,"config");
        String methodPath = FirefightingiotKit.getStr(key,gp)+"?"+"token="+token;
        String domain = ip + methodPath;
        logger.info("------------------------------domain:" + domain + "-----------------------------------------");
        String xml = "";
        xml = HttpUtil.post(domain,jsonParam.toJSONString(1));
        logger.info("------------------------------xml:" + xml + "-----------------------------------------");
        Object object = JSON.parse(xml);
        if(object instanceof com.alibaba.fastjson.JSONObject){
            com.alibaba.fastjson.JSONObject jsonObject = (com.alibaba.fastjson.JSONObject)object;
            if(jsonObject.containsKey("Success") && !jsonObject.getBoolean("Success")){
                return RespEntity.fail(jsonObject.getString("Msg"));
            }
            if(jsonObject.containsKey("result") && !jsonObject.getBoolean("result")){
                return RespEntity.fail(jsonObject.getString("Msg"));
            }
        }else if(object instanceof com.alibaba.fastjson.JSONArray){
            return RespEntity.success(object);
        }
        return RespEntity.success(object);
    }
    /**
     * @Author YangYu
     * @Description 异步消防支队处置
     * @Date Created in 10:05 2022/10/8
     * @Param [jsonParam]
     */

    @Async
    public void restFileHandle(JSONObject jsonParam) throws InterruptedException {
        Thread.sleep(2000);
        JSONObject data = new JSONObject();
        data.put("number",jsonParam.getStr("number"));
        data.put("fireId",jsonParam.getStr("fireId"));
        data.put("handletime",DateUtil.now());
        data.put("handlePerson","值守");
        data.put("handleRemark","通知企业自检");
        data.put("alarmTime",jsonParam.getStr("alarmtime"));
        rest(data,"firefightingiot","restFileHandle","alarmInfo","POST");
    }

    /**
     * @Author YangYu
     * @Description 异步消防支队报警恢复
     * @Date Created in 10:05 2022/10/8
     * @Param [jsonParam]
     */
    @Async
    public void restRecoverFile(JSONObject jsonParam) throws InterruptedException {
        Thread.sleep(2000);
        JSONObject data = new JSONObject();
        data.put("number",jsonParam.getStr("number"));
        data.put("handletime", DateUtil.now());
        rest(data,"firefightingiot","restRecoverFile","alarmInfo","POST");
    }
}
